$(()->
  # 状态预设

  $("#choosePage").hide()
  $(".push-panel").hide()
  $(".copyright").hide()

  selected_count_dom = $(".chooseNum")

  $(".engine-toggle").click (e) ->
    $(".eng-list").fadeToggle 30

  $(".t-label").click (e) ->
    $(".eng-list").fadeOut 30
    _this = $(this)
    $(".engine-toggle").attr "data", _this.attr("data")
    $(".engine-toggle").html _this.html().toString() + "<i class=\"triangle\"></i>"

  $(".search-btn").click (e) ->
    e.stopPropagation()
        # 这里的逻辑好麻烦啊
    unless $.cookie('flowy_token')
      return alert("请先登录")
    if !$(this).hasClass("disabled")
      if task.keyword is ""
        $("BODY").f_popup
          content: "请输入搜索内容！"
          confirm: ->
            $(".search input").focus()
      else
        $.setWaitState true
        $(".push-panel").hide()
        task.new()
        task.keyword = $(".search input").val()
        $(".microblog-list").html('')
        task.search
          success: ->
            $(window).scrollTo $(".screen"), 500

  $(".oops:not(.login)").click (e) ->
  #   html = "<span>填写登录凭证</span><div><input type=\"text\" id=\"luname\"/><a id=\"oopsl_btn\" class=\"oops\">登录</a></div>"
  #   c = $("<div />").addClass("login-alert").html(html)
  #   $("BODY").f_popup
  #     type: "login"
  #     content: c
  #     size:
  #       width: 270

  #     contentPosition: "center"
  # $("#oopsl_btn").click ->
    this_btn = $(this)
    code = window.prompt("请输入口令")
    return false unless code
    $.ajax
      url: "/s/login"
      type: "POST"
      data:
        oops_id: code#$("#luname").val()

      success: (retval)->
        if retval.error
          $("BODY").f_popup
            content: "登陆失败了啊! 请重试 (╯‵□′)╯︵┻━┻ !"
            confirm: ->
        else
          this_btn.addClass('login')
          $(".pulldown-list").show()
          $("BODY").f_popup
            content: "登陆成功！"

            confirm: ->
              $(".search input").focus()
              false

  $('ul.microblog-list').on 'click',('li.available'), () ->
    this_li = $(this)
    _td = $(this).attr('data').split('-')
    this_data =
      pindex: _td[1]
      index:  _td[2]
      wid:  _td[3]
      uid:  _td[4]
      commented: false
    if this_li.hasClass("active")
      this_li.removeClass "active"
      this_li.find(".microblog-panel-top input").prop "checked", false
      task.remove_comment_tweet(this_data)
      selected_count_dom.html(task.selected_count)
    else
      this_li.addClass "active"
      this_li.find(".microblog-panel-top input").prop "checked", true
      task.add_comment_tweet(this_data)
      selected_count_dom.html(task.selected_count)

  $(".microblog-list").delegate "li.available .username", "click", (e) ->
    e.stopPropagation()
    this_li = $(this).closest('li')
    _td = this_li.attr('data').split('-')
    tweet =
      pindex: _td[1]
      index:  _td[2]
      wid:  _td[3]
      uid:  _td[4]
    window.open "http://e.weibo.com/#{tweet.uid}"

  $("a.more").click ->
    $.setWaitState true
    task.next()

  $("a.finished").click (e)->
    e.stopPropagation()

    #滑动到分享区
    $(".push-panel").show()
    $(window).scrollTo $(".push-panel"), 500

  $("a.push-btn").click ->
    unless $(this).hasClass("disabled")
      if task.ready_for_comment().tweets.length == 0
        $("BODY").f_popup content: "您还未勾选微博！"
      else if $(".word-prompt").hasClass("word-over")
        $("BODY").f_popup
          content: "您输入的内容超出字数限制！"
          confirm: ->
            $("#edit-txt").focus()
      else
        $.waitState true, "发送中...", $(".push-btn")
        task.create_comment()



  # 筛选区

  $(".screen").click ->
    $(window).scrollTo $(".screen"), 500

  $(".screen input:checkbox,.screen input:Radio").click ->
    $(".screen-r input").addClass "hover"

  $("#starttime").Zebra_DatePicker
    offset: [ 400, 372 ]
    onSelect: ->
      $(".screen-r input").addClass "hover"
      $("#starthour").removeClass "disabled"

    onClear: ->
      $("#starthour").find("span").attr("value", "0").html "0"
      $("#starthour").addClass "disabled"

    onChange: ->

  $("#endtime").Zebra_DatePicker
    offset: [ 600, 372 ]
    onSelect: ->
      $(".screen-r input").addClass "hover"
      $("#endhour").removeClass "disabled"

    onClear: ->
      $("#endhour").find("span").attr("value", "23").html "23"
      $("#endhour").addClass "disabled"

    onChange: ->

  $(".time").ddl()
  $("#province").next().areaSelect()
  $("input[name='age']").keyup(->
    $(".screen-r input").addClass "hover"  unless isNaN($(this).val())
    $(this).val $(this).val().replace(/\D|^0/g, "")
  ).bind("paste", ->
    $(this).val $(this).val().replace(/\D|^0/g, "")
    $(".screen-r input").addClass "hover"  unless isNaN($(this).val())
  ).css "ime-mode", "disabled"
  $(".screen-r input").click (e) ->
    e.stopPropagation()
    if $(this).hasClass("disabled")

    else
      $.setWaitState true
      $('ul.microblog-list').html('')
      task.filter()

  #nav
  $(".nav .rightSearch").click (e) ->
    e.stopPropagation()
    $(window).scrollTo $(".logo"), 500  unless $(this).hasClass("now")

  $(".nav .rightChoose").click (e) ->
    e.stopPropagation()
    $(window).scrollTo $(".screen"), 500  if not window.bindS and not $(this).hasClass("now")

  $(".nav .rightShare").click (e) ->
    e.stopPropagation()

    #滑动到分享区
    if not window.bindS and not $(this).hasClass("now")
      $(".push-panel").show()
      $(window).scrollTo $(".push-panel"), 500

  $(".check-all input").click (e) ->
    e.stopPropagation()
    if $(this).hasClass("checked") #取消全选
      for tweet_li in $("li.available.active")
        $(tweet_li).removeClass("active").find('input').prop "checked", false
      task.comment_data.tweets = []
      task.selected_count      = 0
      $(".check-all input").removeClass("checked").prop "checked", false
      selected_count_dom.html(0)
    else
      for tweet_li in $("li.available:not(.active)")
        $(tweet_li).click()
      $(".check-all input").addClass("checked").prop "checked", true

  $("#edit-txt").bind "input propertychange", ->

    #字数改变
    regPattern = "(?:([A-Za-z]+):[/]{2})(?:[0-9A-Za-z-]+([.][0-9A-Za-z-]+)+)(?::([0-9]{1,5}))?(?:[/][/0-9A-Za-z_!~*'().;?:@&=+$,%#-]*)?"
    regex = new RegExp(regPattern, "g")
    value = $(this).val()
    match_count = 0
    match_count = value.match(regex).length  if value.match(regex)?
    num = 140 - value.replace(regex, "").length - 10 * match_count

    # var num=140-$(this).val().length;
    if num < 0
      $(".word-prompt").addClass("word-over").html "已超出<span id=\"rest-word\">" + (-num) + "</span>个字"
    else
      $(".word-prompt").removeClass("word-over").html "还能再输入<span id=\"rest-word\">" + num + "</span>个字"

  #######################
  window.task = new Task($.cookie('task_id'))

)